package com.tuyrt.`as`.proj.kotlin_demo

/**
 * author: Administrator on 2021/5/20.
 * desc:
 */

fun main() {
    //fire(ApiGetArticles())
    fire2(ApiService())
}

annotation class ApiDoc(val value: String)

@ApiDoc("修饰类")
class Box {
    @ApiDoc("修饰字段")
    var name: String? = null
}

public enum class Method {
    GET, POST
}

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class HttpMethod(val method: Method)

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class GET(val value: String)

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class POST(val value: String)

interface Api {
    val name: String
    val version: String
        get() = "1.0"
}

@HttpMethod(Method.POST)
class ApiGetArticles : Api {
    override val name: String
        get() = "/api.articles"
}

fun fire(api: Api) {
    val annotations = api.javaClass.annotations
    val method = annotations.find { it is HttpMethod } as? HttpMethod
    println("通过注解得知该接口需要通过：${method?.method}  方式请求")
}


class ApiService {
    @POST("api/page/1")
    fun getSomething() {
    }
}

fun fire2(api: ApiService) {
    api.javaClass.declaredMethods.forEach { method ->
        method.annotations.forEach { annotation ->
            println(annotation.annotationClass.simpleName)
            if (annotation is GET) {
                println("annotation GET value: ${annotation.value}")
            }
            if (annotation is POST) {
                println("annotation POST value: ${annotation.value}")
            }
        }
    }

}